package com.pauldada.sakurabbs.controllers;

import com.pauldada.sakurabbs.dao.CommentRepository;
import com.pauldada.sakurabbs.dao.TaskRepository;
import com.pauldada.sakurabbs.dao.UserAccountRepository;
import com.pauldada.sakurabbs.dto.TaskUserAccount;
import com.pauldada.sakurabbs.entities.Comment;
import com.pauldada.sakurabbs.entities.UserAccount;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;


@Controller
public class IndexController {

    @RequestMapping(value = "/admin")
    public String toAdmin(){
        return "admin/main";
    }

    @RequestMapping("/aboutMe")
    public ModelAndView aboutMe(){
    	ModelAndView mav=new ModelAndView();
    	mav.addObject("title", "关于本站");
    	mav.addObject("mainPage", "common/aboutMe");
    	mav.addObject("mainPageKey", "#a");
    	mav.setViewName("index");
        return mav;
    }

    @RequestMapping("/login")
    public String login(){
        return "login";
    }

    @RequestMapping("/type")
    public ModelAndView root() {
    	ModelAndView mav=new ModelAndView();
    	mav.addObject("title", "首页");
    	mav.addObject("mainPage", "film/indexFilm");
    	mav.addObject("mainPageKey", "#f");
    	mav.setViewName("index");
        return mav;
    }

    @Value("${version}")
    private String version;

    @Autowired
    TaskRepository taskRepository;

    @Autowired
    UserAccountRepository userAccountRepository;

    @Autowired
    CommentRepository commentRepository;

    @GetMapping("/")
    public String DisplayIndex(Model model){

        model.addAttribute("versionNumber",version);

        List<UserAccount> userAccounts = userAccountRepository.findAll();
        model.addAttribute("userAccounts",userAccounts);

        List<TaskUserAccount> taskUserAccounts =taskRepository.taskUserAccounts();
        model.addAttribute("taskUserAccounts", taskUserAccounts);

        List<Comment> comments = commentRepository.findAll();
        model.addAttribute("comments",comments);

        return "main/index";
    }


}
